﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace DHT.Data
{
    public class StringUtils
    {

        /// <summary>
        /// SHA1加密
        /// </summary>
        /// <param name="Ori_string">待加密的数据</param>
        /// <returns></returns>
        public static byte[] SHA1(string Ori_string)
        {
            byte[] inputs = new byte[Ori_string.Length];
            int i = 0;
            foreach (byte b in Ori_string)
            {
                inputs[i] = b;
                i++;
            }

            SHA1 sha = new SHA1CryptoServiceProvider();
            byte[] bts = sha.ComputeHash(inputs);
            return bts;
        }

        public static string SHA1String(string Ori_string)
        {
            byte[] bts = SHA1(Ori_string);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bts)
            {
                //变为2位的16进制的string
                string s = ((int)b).ToString("x");
                if (s.Length == 1)
                {
                    s = "0" + s;
                }
                sb.Append(s);
            }
            return sb.ToString();
        }

    }
}
